<!DOCTYPE html>

<html
  xmlns="http://www.w3.org/1999/xhtml"
  >
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="generator" content="pandoc" />
    <link rel="icon" href="img/logo/data-to-viz.ico" />

    <style>
      .myheader {
        background-image: url("https://github.com/holtzy/data_to_viz/raw/master/img/overview_RGG_faded.png");
        min-height: 350px;
        background-size: 100% 100%;

        /* Create the parallax scrolling effect */
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;

        height: 100%;
        width: 100% !important;
      }
      .mytitle {
        text-transform: uppercase;
        font-family: "Montserrat";
        font-size: 200%;
        font-weight: 500;
        letter-spacing: 4px;
      }
    </style>

    <div class="myheader" style="text-align: center">
      <br />
      <a href="http://www.data-to-viz.com"
        ><img
          align="left"
          src="https://github.com/holtzy/data_to_viz/raw/master/img/logo/typo.png"
          style="width: 150px; vertical-align: top; margin-left: 5%"
      /></a>
      <br /><br />
      <br /><br />
      <p class="mytitle">Visualizing geographic connections</p>
      <center>
        <div class="mysubtitle">
          <i
            >A few data analytics ideas from
            <a href="https://www.data-to-viz.com">Data-to-Viz.com</a></i
          >
        </div>
      </center>
      <br /><br />

            <a href="https://www.data-to-viz.com/#portfolioanchor">
        <img class="mylogoheader"
        src=https://github.com/holtzy/data_to_viz/raw/master/img/section/ConnectedMapSmall.png></a
      >
                   </div>

       
    <title>MapConnection.knit</title>

     <script src="libs/header-attrs-2.27/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap-3.3.5/css/lumen.min.css" rel="stylesheet" />
<script src="libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
       h1.title {font-size: 38px;}
       h2 {font-size: 30px;}
       h3 {font-size: 24px;}
       h4 {font-size: 18px;}
       h5 {font-size: 16px;}
       h6 {font-size: 12px;}
       code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
       pre:not([class]) { background-color: white }</style>
<script src="libs/navigation-1.1/tabsets.js"></script>
<script src="libs/navigation-1.1/codefolding.js"></script>
<link href="libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet" />
<script src="libs/pagedtable-1.1/js/pagedtable.js"></script>
<script src="libs/kePrint-0.0.1/kePrint.js"></script>
<link href="libs/lightable-0.0.1/lightable.css" rel="stylesheet" />       <style type="text/css">
      code {
        white-space: pre;
      }
    </style>
    <style type="text/css">
      pre > code.sourceCode { white-space: pre; position: relative; }
      pre > code.sourceCode > span { line-height: 1.25; }
      pre > code.sourceCode > span:empty { height: 1.2em; }
      .sourceCode { overflow: visible; }
      code.sourceCode > span { color: inherit; text-decoration: inherit; }
      div.sourceCode { margin: 1em 0; }
      pre.sourceCode { margin: 0; }
      @media screen {
      div.sourceCode { overflow: auto; }
      }
      @media print {
      pre > code.sourceCode { white-space: pre-wrap; }
      pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
      }
      pre.numberSource code
        { counter-reset: source-line 0; }
      pre.numberSource code > span
        { position: relative; left: -4em; counter-increment: source-line; }
      pre.numberSource code > span > a:first-child::before
        { content: counter(source-line);
          position: relative; left: -1em; text-align: right; vertical-align: baseline;
          border: none; display: inline-block;
          -webkit-touch-callout: none; -webkit-user-select: none;
          -khtml-user-select: none; -moz-user-select: none;
          -ms-user-select: none; user-select: none;
          padding: 0 4px; width: 4em;
          color: #aaaaaa;
        }
      pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
      div.sourceCode
        {   }
      @media screen {
      pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
      }
      code span.al { color: #ff0000; font-weight: bold; } /* Alert */
      code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
      code span.at { color: #7d9029; } /* Attribute */
      code span.bn { color: #40a070; } /* BaseN */
      code span.bu { color: #008000; } /* BuiltIn */
      code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
      code span.ch { color: #4070a0; } /* Char */
      code span.cn { color: #880000; } /* Constant */
      code span.co { color: #60a0b0; font-style: italic; } /* Comment */
      code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
      code span.do { color: #ba2121; font-style: italic; } /* Documentation */
      code span.dt { color: #902000; } /* DataType */
      code span.dv { color: #40a070; } /* DecVal */
      code span.er { color: #ff0000; font-weight: bold; } /* Error */
      code span.ex { } /* Extension */
      code span.fl { color: #40a070; } /* Float */
      code span.fu { color: #06287e; } /* Function */
      code span.im { color: #008000; font-weight: bold; } /* Import */
      code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
      code span.kw { color: #007020; font-weight: bold; } /* Keyword */
      code span.op { color: #666666; } /* Operator */
      code span.ot { color: #007020; } /* Other */
      code span.pp { color: #bc7a00; } /* Preprocessor */
      code span.sc { color: #4070a0; } /* SpecialChar */
      code span.ss { color: #bb6688; } /* SpecialString */
      code span.st { color: #4070a0; } /* String */
      code span.va { color: #19177c; } /* Variable */
      code span.vs { color: #4070a0; } /* VerbatimString */
      code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
            div.sourceCode {
        overflow-x: visible;
      }
          </style>
        <style type="text/css">
      pre:not([class]) {
        background-color: white;
      }
    </style>
           <style type="text/css">
      h1 {
        font-size: 34px;
      }
      h1.title {
        font-size: 38px;
      }
      h2 {
        font-size: 30px;
      }
      h3 {
        font-size: 24px;
      }
      h4 {
        font-size: 18px;
      }
      h5 {
        font-size: 16px;
      }
      h6 {
        font-size: 12px;
      }
      .table th:not([align]) {
        text-align: left;
      }
          </style>
         <link
      rel="stylesheet"
      href="style.css"
      type="text/css"
      
    />
      </head>

  <body>
        <style type="text/css">
      .main-container {
        max-width: 940px;
        margin-left: auto;
        margin-right: auto;
      }
      code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
      }
      img {
        max-width: 100%;
        height: auto;
      }
      .tabbed-pane {
        padding-top: 12px;
      }
      button.code-folding-btn:focus {
        outline: none;
      }
    </style>

     
    <div class="container-fluid main-container">
      <!-- tabsets -->
      <script>
        $(document).ready(function () {
          window.buildTabsets("TOC");
        });
      </script>

      <!-- code folding -->
            <style type="text/css">
        .code-folding-btn {
          margin-bottom: 4px;
        }
      </style>
      <script>
        $(document).ready(function () {
                          window.initializeCodeFolding("hide" === "show");
                });
      </script>
         
                    <div class="fluid-row" id="header">
                        <div class="btn-group pull-right">
              <button
                type="button"
                class="btn btn-default btn-xs dropdown-toggle"
                data-toggle="dropdown"
                aria-haspopup="true"
                aria-expanded="false"
              >
                <span>Code</span> <span class="caret"></span>
              </button>
              <ul class="dropdown-menu" style="min-width: 50px">
                                <li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
                <li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
                                </ul>
            </div>

                         </div>
              <br><br>
<div class="mycontent">
<p>This document provides a few suggestions for the visualization of
geographical connections.</p>
<p>The dataset considered here is available on <a
href="https://github.com/holtzy/data_to_viz/tree/master/Example_dataset">github</a>.
It is based on about 13,000 tweets containing the <a
href="https://twitter.com/search?q=%23surf&amp;src=typd&amp;lang=en">#surf</a>
hashtag. These tweets have been recovered on a 10 months period, and
those with both current geo location and correct city of origin have
been kept. You can learn more on this project in this dedicated <a
href="https://github.com/holtzy/About-Surfers-On-Twitter">github
repository</a>.</p>
<p>The dataset provides longitude and latitude for both the home
location of tweeters, and their instant geolocation as well. Basically
it looks like that:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># Libraries</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes)</span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">library</span>(viridis)</span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="fu">library</span>(DT)</span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="fu">library</span>(kableExtra)</span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="fu">options</span>(<span class="at">knitr.table.format =</span> <span class="st">&quot;html&quot;</span>)</span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="fu">library</span>(jpeg)</span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="fu">library</span>(maps)</span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="fu">library</span>(geosphere)</span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="fu">library</span>(grid)</span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a></span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a><span class="co">#load(url(&quot;https://github.com/holtzy/About-Surfers-On-Twitter/raw/master/DATA/Surf_Hashtag_Data.Rdata&quot;))</span></span>
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a><span class="co">#data &lt;- data %&gt;% select(homename, homecontinent, homecountry, homelat, homelon, travelcontinent, travelcountry, travellat, travellon) %&gt;% na.omit()</span></span>
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a><span class="co">#write.table(data, file=&quot;/Users/y.holtz/Dropbox/data_to_viz/Example_dataset/19_MapConnection.csv&quot;, quote=TRUE, row.names=FALSE, sep=&quot;,&quot;)  </span></span>
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a></span>
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a><span class="co"># Load dataset from github (Surfer project)</span></span>
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">&quot;https://github.com/holtzy/data_to_viz/raw/master/Example_dataset/19_MapConnection.csv&quot;</span>, <span class="at">header=</span>T, <span class="at">sep=</span><span class="st">&quot;,&quot;</span>)</span>
<span id="cb1-19"><a href="#cb1-19" tabindex="-1"></a></span>
<span id="cb1-20"><a href="#cb1-20" tabindex="-1"></a><span class="co"># Show long format</span></span>
<span id="cb1-21"><a href="#cb1-21" tabindex="-1"></a>tmp <span class="ot">&lt;-</span> data <span class="sc">%&gt;%</span></span>
<span id="cb1-22"><a href="#cb1-22" tabindex="-1"></a>  <span class="fu">tail</span>(<span class="dv">5</span>) <span class="sc">%&gt;%</span></span>
<span id="cb1-23"><a href="#cb1-23" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">homename =</span> <span class="fu">gsub</span>( <span class="st">&quot;, Western Australia&quot;</span>, <span class="st">&quot;&quot;</span>, homename)) <span class="sc">%&gt;%</span></span>
<span id="cb1-24"><a href="#cb1-24" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">homename =</span> <span class="fu">gsub</span>( <span class="st">&quot;, France&quot;</span>, <span class="st">&quot;&quot;</span>, homename)) <span class="sc">%&gt;%</span></span>
<span id="cb1-25"><a href="#cb1-25" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">homename =</span> <span class="fu">gsub</span>( <span class="st">&quot; - Bali - Indonesia&quot;</span>, <span class="st">&quot;&quot;</span>, homename)) <span class="sc">%&gt;%</span></span>
<span id="cb1-26"><a href="#cb1-26" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">homelat=</span><span class="fu">round</span>(homelat,<span class="dv">1</span>), <span class="at">homelon=</span><span class="fu">round</span>(homelon,<span class="dv">1</span>), <span class="at">travellat=</span><span class="fu">round</span>(travellat,<span class="dv">1</span>), <span class="at">travellon=</span><span class="fu">round</span>(travellon,<span class="dv">1</span>)) <span class="sc">%&gt;%</span></span>
<span id="cb1-27"><a href="#cb1-27" tabindex="-1"></a>  <span class="fu">select</span>(homename, homelat, homelon, travelcountry, travellat, travellon)</span>
<span id="cb1-28"><a href="#cb1-28" tabindex="-1"></a></span>
<span id="cb1-29"><a href="#cb1-29" tabindex="-1"></a>tmp <span class="sc">%&gt;%</span> <span class="fu">kable</span>() <span class="sc">%&gt;%</span> <span class="fu">kable_styling</span>(<span class="at">bootstrap_options =</span> <span class="st">&quot;striped&quot;</span>, <span class="at">full_width =</span> F)</span></code></pre></div>
<table class="table table-striped" style="width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
</th>
<th style="text-align:left;">
homename
</th>
<th style="text-align:right;">
homelat
</th>
<th style="text-align:right;">
homelon
</th>
<th style="text-align:left;">
travelcountry
</th>
<th style="text-align:right;">
travellat
</th>
<th style="text-align:right;">
travellon
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
9507
</td>
<td style="text-align:left;">
Bridgetown
</td>
<td style="text-align:right;">
-34.0
</td>
<td style="text-align:right;">
116.1
</td>
<td style="text-align:left;">
Australia
</td>
<td style="text-align:right;">
-34.2
</td>
<td style="text-align:right;">
115.0
</td>
</tr>
<tr>
<td style="text-align:left;">
9508
</td>
<td style="text-align:left;">
Lille
</td>
<td style="text-align:right;">
50.6
</td>
<td style="text-align:right;">
3.1
</td>
<td style="text-align:left;">
France
</td>
<td style="text-align:right;">
45.0
</td>
<td style="text-align:right;">
-1.2
</td>
</tr>
<tr>
<td style="text-align:left;">
9509
</td>
<td style="text-align:left;">
MX
</td>
<td style="text-align:right;">
23.6
</td>
<td style="text-align:right;">
-102.6
</td>
<td style="text-align:left;">
Mexico
</td>
<td style="text-align:right;">
21.0
</td>
<td style="text-align:right;">
-101.2
</td>
</tr>
<tr>
<td style="text-align:left;">
9510
</td>
<td style="text-align:left;">
Kuta
</td>
<td style="text-align:right;">
-8.7
</td>
<td style="text-align:right;">
115.2
</td>
<td style="text-align:left;">
Indonesia
</td>
<td style="text-align:right;">
-8.7
</td>
<td style="text-align:right;">
115.2
</td>
</tr>
<tr>
<td style="text-align:left;">
9511
</td>
<td style="text-align:left;">
Kuta
</td>
<td style="text-align:right;">
-8.7
</td>
<td style="text-align:right;">
115.2
</td>
<td style="text-align:left;">
Indonesia
</td>
<td style="text-align:right;">
-8.7
</td>
<td style="text-align:right;">
115.2
</td>
</tr>
</tbody>
</table>
<p><br><br></p>
<div id="showing-a-few-connection-on-a-map" class="section level1">
<h1>Showing a few connection on a map</h1>
<hr />
<p>Before showing all the relationships provided in this dataset, it is
important to understand how to visualize a unique connection on a map.
It is a common practice to link 2 points using the shortest route
between them instead of a straight line. It is called <a
href="https://en.wikipedia.org/wiki/Great_circle">great circles</a>. A
special care is given for situations where cities are very far from each
other and where the shortest connection thus passes behind the map.</p>
<p>Here are the connection between 7 major cities on a world map:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>don<span class="ot">=</span><span class="fu">rbind</span>(</span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a>    <span class="at">Paris=</span><span class="fu">c</span>(<span class="dv">2</span>,<span class="dv">49</span>),</span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a>    <span class="at">Melbourne=</span><span class="fu">c</span>(<span class="dv">145</span>,<span class="sc">-</span><span class="dv">38</span>),</span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a>    <span class="at">Saint.Petersburg=</span><span class="fu">c</span>(<span class="fl">30.32</span>, <span class="fl">59.93</span>),</span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a>    <span class="at">Abidjan=</span><span class="fu">c</span>(<span class="sc">-</span><span class="fl">4.03</span>, <span class="fl">5.33</span>),</span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a>    <span class="at">Montreal=</span><span class="fu">c</span>(<span class="sc">-</span><span class="fl">73.57</span>, <span class="fl">45.52</span>),</span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a>    <span class="at">Nairobi=</span><span class="fu">c</span>(<span class="fl">36.82</span>, <span class="sc">-</span><span class="fl">1.29</span>),</span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a>    <span class="at">Salvador=</span><span class="fu">c</span>(<span class="sc">-</span><span class="fl">38.5</span>, <span class="sc">-</span><span class="fl">12.97</span>)</span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a>    )  <span class="sc">%&gt;%</span> <span class="fu">as.data.frame</span>()</span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="fu">colnames</span>(don)<span class="ot">=</span><span class="fu">c</span>(<span class="st">&quot;long&quot;</span>,<span class="st">&quot;lat&quot;</span>)</span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a></span>
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a></span>
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a>all_pairs<span class="ot">=</span><span class="fu">cbind</span>(<span class="fu">t</span>(<span class="fu">combn</span>(don<span class="sc">$</span>long, <span class="dv">2</span>)), <span class="fu">t</span>(<span class="fu">combn</span>(don<span class="sc">$</span>lat, <span class="dv">2</span>))) <span class="sc">%&gt;%</span> <span class="fu">as.data.frame</span>()</span>
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a><span class="fu">colnames</span>(all_pairs)<span class="ot">=</span><span class="fu">c</span>(<span class="st">&quot;long1&quot;</span>,<span class="st">&quot;long2&quot;</span>,<span class="st">&quot;lat1&quot;</span>,<span class="st">&quot;lat2&quot;</span>)</span>
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a></span>
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a>plot_my_connection<span class="ot">=</span><span class="cf">function</span>( dep_lon, dep_lat, arr_lon, arr_lat, ...){</span>
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a>    inter <span class="ot">&lt;-</span> <span class="fu">gcIntermediate</span>(<span class="fu">c</span>(dep_lon, dep_lat), <span class="fu">c</span>(arr_lon, arr_lat), <span class="at">n=</span><span class="dv">50</span>, <span class="at">addStartEnd=</span><span class="cn">TRUE</span>, <span class="at">breakAtDateLine=</span>F)             </span>
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a>    inter<span class="ot">=</span><span class="fu">data.frame</span>(inter)</span>
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a>    diff_of_lon<span class="ot">=</span><span class="fu">abs</span>(dep_lon) <span class="sc">+</span> <span class="fu">abs</span>(arr_lon)</span>
<span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a>    <span class="cf">if</span>(diff_of_lon <span class="sc">&gt;</span> <span class="dv">180</span>){</span>
<span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a>        <span class="fu">lines</span>(<span class="fu">subset</span>(inter, lon<span class="sc">&gt;=</span><span class="dv">0</span>), ...)</span>
<span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a>        <span class="fu">lines</span>(<span class="fu">subset</span>(inter, lon<span class="sc">&lt;</span><span class="dv">0</span>), ...)</span>
<span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a>    }<span class="cf">else</span>{</span>
<span id="cb2-24"><a href="#cb2-24" tabindex="-1"></a>        <span class="fu">lines</span>(inter, ...)</span>
<span id="cb2-25"><a href="#cb2-25" tabindex="-1"></a>        }</span>
<span id="cb2-26"><a href="#cb2-26" tabindex="-1"></a>}</span>
<span id="cb2-27"><a href="#cb2-27" tabindex="-1"></a></span>
<span id="cb2-28"><a href="#cb2-28" tabindex="-1"></a><span class="co"># background map</span></span>
<span id="cb2-29"><a href="#cb2-29" tabindex="-1"></a><span class="fu">par</span>(<span class="at">mar=</span><span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>))</span>
<span id="cb2-30"><a href="#cb2-30" tabindex="-1"></a><span class="fu">map</span>(<span class="st">&#39;world&#39;</span>,<span class="at">col=</span><span class="st">&quot;#f2f2f2&quot;</span>, <span class="at">fill=</span><span class="cn">TRUE</span>, <span class="at">bg=</span><span class="st">&quot;white&quot;</span>, <span class="at">lwd=</span><span class="fl">0.05</span>,<span class="at">mar=</span><span class="fu">rep</span>(<span class="dv">0</span>,<span class="dv">4</span>),<span class="at">border=</span><span class="dv">0</span>, <span class="at">ylim=</span><span class="fu">c</span>(<span class="sc">-</span><span class="dv">80</span>,<span class="dv">80</span>) )</span>
<span id="cb2-31"><a href="#cb2-31" tabindex="-1"></a> </span>
<span id="cb2-32"><a href="#cb2-32" tabindex="-1"></a><span class="co"># add every connections:</span></span>
<span id="cb2-33"><a href="#cb2-33" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">nrow</span>(all_pairs)){</span>
<span id="cb2-34"><a href="#cb2-34" tabindex="-1"></a>    <span class="fu">plot_my_connection</span>(all_pairs<span class="sc">$</span>long1[i], all_pairs<span class="sc">$</span>lat1[i], all_pairs<span class="sc">$</span>long2[i], all_pairs<span class="sc">$</span>lat2[i], <span class="at">col=</span><span class="st">&quot;#69b3a2&quot;</span>, <span class="at">lwd=</span><span class="dv">1</span>)</span>
<span id="cb2-35"><a href="#cb2-35" tabindex="-1"></a>    }</span>
<span id="cb2-36"><a href="#cb2-36" tabindex="-1"></a> </span>
<span id="cb2-37"><a href="#cb2-37" tabindex="-1"></a><span class="co"># add points and names of cities</span></span>
<span id="cb2-38"><a href="#cb2-38" tabindex="-1"></a><span class="fu">points</span>(<span class="at">x=</span>don<span class="sc">$</span>long, <span class="at">y=</span>don<span class="sc">$</span>lat, <span class="at">col=</span><span class="st">&quot;#69b3a2&quot;</span>, <span class="at">cex=</span><span class="dv">2</span>, <span class="at">pch=</span><span class="dv">20</span>)</span>
<span id="cb2-39"><a href="#cb2-39" tabindex="-1"></a><span class="fu">text</span>(<span class="fu">rownames</span>(don), <span class="at">x=</span>don<span class="sc">$</span>long, <span class="at">y=</span>don<span class="sc">$</span>lat,  <span class="at">col=</span><span class="st">&quot;black&quot;</span>, <span class="at">cex=</span><span class="dv">1</span>, <span class="at">pos=</span><span class="dv">4</span>)</span></code></pre></div>
<p><img src="MapConnection_files/figure-html/unnamed-chunk-2-1.png" width="960" style="display: block; margin: auto;" /></p>
</div>
<div id="showing-many-connections-on-a-map" class="section level1">
<h1>Showing many connections on a map</h1>
<hr />
<p>It is then possible to use the same method for the whole dataset
composed of about 10,000 connections. With such a sample size, it makes
sense to group the connections that have exactly the same starting and
ending points. Then it is important to represent the connections with
high volume on top of the graphic, and those with small volume below it.
Indeed this will allow to highlight the most important pattern and hide
the noise of rare connections.</p>
<p>Here I choosed to use a <a
href="https://raw.githubusercontent.com/holtzy/data_to_viz/master/story/IMG/BlackMarble_2016_01deg.jpg">NASA
night lights</a> image as a background, inspired from this <a
href="https://www.dataplanes.org/notes/2018/01/27/flight-routes-night-lights">blog
post</a>.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="co"># Load dataset from github (Surfer project)</span></span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">&quot;https://github.com/holtzy/data_to_viz/raw/master/Example_dataset/19_MapConnection.csv&quot;</span>, <span class="at">header=</span>T, <span class="at">sep=</span><span class="st">&quot;,&quot;</span>)</span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co"># Download NASA night lights image</span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="fu">download.file</span>(<span class="st">&quot;https://raw.githubusercontent.com/holtzy/data_to_viz/master/story/IMG/BlackMarble_2016_01deg.jpg&quot;</span>, </span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="at">destfile =</span> <span class="st">&quot;IMG/BlackMarble_2016_01deg.jpg&quot;</span>, <span class="at">mode =</span> <span class="st">&quot;wb&quot;</span>)</span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co"># Load picture and render</span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a>earth <span class="ot">&lt;-</span> <span class="fu">readJPEG</span>(<span class="st">&quot;IMG/BlackMarble_2016_01deg.jpg&quot;</span>, <span class="at">native =</span> <span class="cn">TRUE</span>)</span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a>earth <span class="ot">&lt;-</span> <span class="fu">rasterGrob</span>(earth, <span class="at">interpolate =</span> <span class="cn">TRUE</span>)</span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a></span>
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co"># Count how many times we have each unique connexion + order by importance</span></span>
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a>summary<span class="ot">=</span>data <span class="sc">%&gt;%</span> </span>
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a>  dplyr<span class="sc">::</span><span class="fu">count</span>(homelat,homelon,homecontinent, travellat,travellon,travelcontinent) <span class="sc">%&gt;%</span></span>
<span id="cb3-14"><a href="#cb3-14" tabindex="-1"></a>  <span class="fu">arrange</span>(n)</span>
<span id="cb3-15"><a href="#cb3-15" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" tabindex="-1"></a><span class="co"># A function that makes a dateframe per connection (we will use these connections to plot each lines)</span></span>
<span id="cb3-17"><a href="#cb3-17" tabindex="-1"></a>data_for_connection<span class="ot">=</span><span class="cf">function</span>( dep_lon, dep_lat, arr_lon, arr_lat, group){</span>
<span id="cb3-18"><a href="#cb3-18" tabindex="-1"></a>  inter <span class="ot">&lt;-</span> <span class="fu">gcIntermediate</span>(<span class="fu">c</span>(dep_lon, dep_lat), <span class="fu">c</span>(arr_lon, arr_lat), <span class="at">n=</span><span class="dv">50</span>, <span class="at">addStartEnd=</span><span class="cn">TRUE</span>, <span class="at">breakAtDateLine=</span>F)             </span>
<span id="cb3-19"><a href="#cb3-19" tabindex="-1"></a>  inter<span class="ot">=</span><span class="fu">data.frame</span>(inter)</span>
<span id="cb3-20"><a href="#cb3-20" tabindex="-1"></a>  inter<span class="sc">$</span>group<span class="ot">=</span><span class="cn">NA</span></span>
<span id="cb3-21"><a href="#cb3-21" tabindex="-1"></a>  diff_of_lon<span class="ot">=</span><span class="fu">abs</span>(dep_lon) <span class="sc">+</span> <span class="fu">abs</span>(arr_lon)</span>
<span id="cb3-22"><a href="#cb3-22" tabindex="-1"></a>  <span class="cf">if</span>(diff_of_lon <span class="sc">&gt;</span> <span class="dv">180</span>){</span>
<span id="cb3-23"><a href="#cb3-23" tabindex="-1"></a>    inter<span class="sc">$</span>group[ <span class="fu">which</span>(inter<span class="sc">$</span>lon<span class="sc">&gt;=</span><span class="dv">0</span>)]<span class="ot">=</span><span class="fu">paste</span>(group, <span class="st">&quot;A&quot;</span>,<span class="at">sep=</span><span class="st">&quot;&quot;</span>)</span>
<span id="cb3-24"><a href="#cb3-24" tabindex="-1"></a>    inter<span class="sc">$</span>group[ <span class="fu">which</span>(inter<span class="sc">$</span>lon<span class="sc">&lt;</span><span class="dv">0</span>)]<span class="ot">=</span><span class="fu">paste</span>(group, <span class="st">&quot;B&quot;</span>,<span class="at">sep=</span><span class="st">&quot;&quot;</span>)</span>
<span id="cb3-25"><a href="#cb3-25" tabindex="-1"></a>  }<span class="cf">else</span>{</span>
<span id="cb3-26"><a href="#cb3-26" tabindex="-1"></a>    inter<span class="sc">$</span>group<span class="ot">=</span>group</span>
<span id="cb3-27"><a href="#cb3-27" tabindex="-1"></a>  }</span>
<span id="cb3-28"><a href="#cb3-28" tabindex="-1"></a>  <span class="fu">return</span>(inter)</span>
<span id="cb3-29"><a href="#cb3-29" tabindex="-1"></a>}</span>
<span id="cb3-30"><a href="#cb3-30" tabindex="-1"></a></span>
<span id="cb3-31"><a href="#cb3-31" tabindex="-1"></a><span class="co"># Création d&#39;un dataframe complet avec les points de toutes les lignes à faire.</span></span>
<span id="cb3-32"><a href="#cb3-32" tabindex="-1"></a>data_ready_plot<span class="ot">=</span><span class="fu">data.frame</span>()</span>
<span id="cb3-33"><a href="#cb3-33" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="fu">c</span>(<span class="dv">1</span><span class="sc">:</span><span class="fu">nrow</span>(summary))){</span>
<span id="cb3-34"><a href="#cb3-34" tabindex="-1"></a>  tmp<span class="ot">=</span><span class="fu">data_for_connection</span>(summary<span class="sc">$</span>homelon[i], summary<span class="sc">$</span>homelat[i], summary<span class="sc">$</span>travellon[i], summary<span class="sc">$</span>travellat[i] , i)</span>
<span id="cb3-35"><a href="#cb3-35" tabindex="-1"></a>  tmp<span class="sc">$</span>homecontinent<span class="ot">=</span>summary<span class="sc">$</span>homecontinent[i]</span>
<span id="cb3-36"><a href="#cb3-36" tabindex="-1"></a>  tmp<span class="sc">$</span>n<span class="ot">=</span>summary<span class="sc">$</span>n[i]</span>
<span id="cb3-37"><a href="#cb3-37" tabindex="-1"></a>  data_ready_plot<span class="ot">=</span><span class="fu">rbind</span>(data_ready_plot, tmp)</span>
<span id="cb3-38"><a href="#cb3-38" tabindex="-1"></a>}</span>
<span id="cb3-39"><a href="#cb3-39" tabindex="-1"></a>data_ready_plot<span class="sc">$</span>homecontinent<span class="ot">=</span><span class="fu">factor</span>(data_ready_plot<span class="sc">$</span>homecontinent, <span class="at">levels=</span><span class="fu">c</span>(<span class="st">&quot;Asia&quot;</span>,<span class="st">&quot;Europe&quot;</span>,<span class="st">&quot;Australia&quot;</span>,<span class="st">&quot;Africa&quot;</span>,<span class="st">&quot;North America&quot;</span>,<span class="st">&quot;South America&quot;</span>,<span class="st">&quot;Antarctica&quot;</span>))</span>
<span id="cb3-40"><a href="#cb3-40" tabindex="-1"></a></span>
<span id="cb3-41"><a href="#cb3-41" tabindex="-1"></a><span class="co"># Plot</span></span>
<span id="cb3-42"><a href="#cb3-42" tabindex="-1"></a>p <span class="ot">&lt;-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb3-43"><a href="#cb3-43" tabindex="-1"></a>  <span class="fu">annotation_custom</span>(earth, <span class="at">xmin =</span> <span class="sc">-</span><span class="dv">180</span>, <span class="at">xmax =</span> <span class="dv">180</span>, <span class="at">ymin =</span> <span class="sc">-</span><span class="dv">90</span>, <span class="at">ymax =</span> <span class="dv">90</span>) <span class="sc">+</span></span>
<span id="cb3-44"><a href="#cb3-44" tabindex="-1"></a>  <span class="fu">geom_line</span>(<span class="at">data=</span>data_ready_plot, <span class="fu">aes</span>(<span class="at">x=</span>lon, <span class="at">y=</span>lat, <span class="at">group=</span>group, <span class="at">colour=</span>homecontinent, <span class="at">alpha=</span>n), <span class="at">size=</span><span class="fl">0.6</span>) <span class="sc">+</span></span>
<span id="cb3-45"><a href="#cb3-45" tabindex="-1"></a>  <span class="fu">scale_color_brewer</span>(<span class="at">palette=</span><span class="st">&quot;Set3&quot;</span>) <span class="sc">+</span></span>
<span id="cb3-46"><a href="#cb3-46" tabindex="-1"></a>  <span class="fu">theme_void</span>() <span class="sc">+</span></span>
<span id="cb3-47"><a href="#cb3-47" tabindex="-1"></a>  <span class="fu">theme</span>(</span>
<span id="cb3-48"><a href="#cb3-48" tabindex="-1"></a>        <span class="at">legend.position=</span><span class="st">&quot;none&quot;</span>,</span>
<span id="cb3-49"><a href="#cb3-49" tabindex="-1"></a>        <span class="at">panel.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;black&quot;</span>, <span class="at">colour =</span> <span class="st">&quot;black&quot;</span>), </span>
<span id="cb3-50"><a href="#cb3-50" tabindex="-1"></a>        <span class="at">panel.spacing=</span><span class="fu">unit</span>(<span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>), <span class="st">&quot;null&quot;</span>),</span>
<span id="cb3-51"><a href="#cb3-51" tabindex="-1"></a>        <span class="at">plot.margin=</span>grid<span class="sc">::</span><span class="fu">unit</span>(<span class="fu">c</span>(<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>,<span class="dv">0</span>), <span class="st">&quot;cm&quot;</span>),</span>
<span id="cb3-52"><a href="#cb3-52" tabindex="-1"></a>  ) <span class="sc">+</span></span>
<span id="cb3-53"><a href="#cb3-53" tabindex="-1"></a>  ggplot2<span class="sc">::</span><span class="fu">annotate</span>(<span class="st">&quot;text&quot;</span>, <span class="at">x =</span> <span class="sc">-</span><span class="dv">150</span>, <span class="at">y =</span> <span class="sc">-</span><span class="dv">45</span>, <span class="at">hjust =</span> <span class="dv">0</span>, <span class="at">size =</span> <span class="dv">11</span>, <span class="at">label =</span> <span class="fu">paste</span>(<span class="st">&quot;Where surfers travel.&quot;</span>), <span class="at">color =</span> <span class="st">&quot;white&quot;</span>) <span class="sc">+</span></span>
<span id="cb3-54"><a href="#cb3-54" tabindex="-1"></a>  ggplot2<span class="sc">::</span><span class="fu">annotate</span>(<span class="st">&quot;text&quot;</span>, <span class="at">x =</span> <span class="sc">-</span><span class="dv">150</span>, <span class="at">y =</span> <span class="sc">-</span><span class="dv">51</span>, <span class="at">hjust =</span> <span class="dv">0</span>, <span class="at">size =</span> <span class="dv">8</span>, <span class="at">label =</span> <span class="fu">paste</span>(<span class="st">&quot;data-to-viz.com | NASA.gov | 10,000 #surf tweets recovered&quot;</span>), <span class="at">color =</span> <span class="st">&quot;white&quot;</span>, <span class="at">alpha =</span> <span class="fl">0.5</span>) <span class="sc">+</span></span>
<span id="cb3-55"><a href="#cb3-55" tabindex="-1"></a>  <span class="co">#ggplot2::annotate(&quot;text&quot;, x = 160, y = -51, hjust = 1, size = 7, label = paste(&quot;Cacedédi Air-Guimzu 2018&quot;), color = &quot;white&quot;, alpha = 0.5) +</span></span>
<span id="cb3-56"><a href="#cb3-56" tabindex="-1"></a>  <span class="fu">xlim</span>(<span class="sc">-</span><span class="dv">180</span>,<span class="dv">180</span>) <span class="sc">+</span></span>
<span id="cb3-57"><a href="#cb3-57" tabindex="-1"></a>  <span class="fu">ylim</span>(<span class="sc">-</span><span class="dv">60</span>,<span class="dv">80</span>) <span class="sc">+</span></span>
<span id="cb3-58"><a href="#cb3-58" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">expand =</span> <span class="fu">c</span>(<span class="fl">0.006</span>, <span class="fl">0.006</span>)) <span class="sc">+</span></span>
<span id="cb3-59"><a href="#cb3-59" tabindex="-1"></a>  <span class="fu">coord_equal</span>() </span>
<span id="cb3-60"><a href="#cb3-60" tabindex="-1"></a></span>
<span id="cb3-61"><a href="#cb3-61" tabindex="-1"></a><span class="co"># Save at PNG</span></span>
<span id="cb3-62"><a href="#cb3-62" tabindex="-1"></a><span class="fu">ggsave</span>(<span class="st">&quot;IMG/Surfer_travel.png&quot;</span>, <span class="at">width =</span> <span class="dv">36</span>, <span class="at">height =</span> <span class="fl">15.22</span>, <span class="at">units =</span> <span class="st">&quot;in&quot;</span>, <span class="at">dpi =</span> <span class="dv">90</span>)</span></code></pre></div>
<center>
<p><img src="IMG/Surfer_travel.png" /></p>
</center>
<p><br><br></p>
<p>Please note that this map is available <a
href="https://github.com/holtzy/data_to_viz/tree/master/story/IMG">here</a>
if needed. Even if a connecting map is probably the best option for
plotting this kind of dataset, please note that other representation
like chord diagrams or networks could make a good job as well.</p>
</div>
</div>
<div class="mycontent">









          <!-- Insert code for related graphics  -->
          <h1>Going further</h1>
          <hr />
          <p>
            You can learn more about each type of graphic presented in this
            story in the dedicated sections. Click the icon below:
          </p>
                    <a href="https://www.data-to-viz.com/#portfolio">
            <img class="mylogoheader"
            src=https://github.com/holtzy/data_to_viz/raw/master/img/section/ConnectedMapSmall.png></a
          >
                         </div>
    </div>
    <section
      id="form"
      style="
        margin-top: 70px;
        padding-bottom: 70px;
        padding-top: 70px;
        background-color: #f8f9fa;
      "
    >
      <div class="container">
        <div style="display: flex; flex-direction: column; align-items: center">
          <h2 style="font-size: 44px" class="text-center">
            Dataviz decision tree
          </h2>
          <p class="text-center" style="max-width: 600px; font-size: 18px">
            <a href="https://www.data-to-viz.com">Data To Viz</a> is a
            comprehensive <b>classification of chart types</b> organized by data
            input format. Get a high-resolution version of our decision tree
            delivered to your inbox now!
          </p>
          <div
            style="
              margin-top: -50px;
              width: 100%;
              display: flex;
              justify-content: center;
            "
          >
            <script
              async
              data-uid="8010f15fd2"
              src="https://prodigious-trailblazer-3628.ck.page/8010f15fd2/index.js"
            ></script>
          </div>
          <br />
          <div class="text-center">
            <img
              class="img-fluid"
              src="../img/poster/poster_small.png"
              alt="High Resolution Poster"
              style="max-width: 400px"
            />
          </div>
        </div>
      </div>
    </section>

     &nbsp;
<hr />
<p style="text-align: center;">A work by <a href="https://github.com/holtzy/">Yan Holtz</a> for <a href="https://data-to-viz.com">data-to-viz.com</a></p>

<!-- Add icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Add font awesome icons -->
<p style="text-align: center;">
	<a href="https://twitter.com/r_graph_gallery?lang=en" class="fa fa-twitter"></a>
	<a href="https://www.linkedin.com/in/yan-holtz-2477534a/" class="fa fa-linkedin"></a>
	<a href="https://github.com/holtzy/" class="fa fa-github"></a>
</p>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-79254642-3"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-79254642-3');
</script>
<script>
var getOutboundLink = function(url) {
	gtag('event', 'click', {
		'event_category': 'outbound',
		'event_label': url,
		'transport_type': 'beacon',
		'event_callback': function(){document.location = url;}
	});
}
</script>

&nbsp;   
    <script>

      // add bootstrap table styles to pandoc tables
      function bootstrapStylePandocTables() {
        $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
      }
      $(document).ready(function () {
        bootstrapStylePandocTables();
      });

          </script>
       </body>
</html>
